package net.chasing.androidbaseconfig.util;

import android.annotation.SuppressLint;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public class ViewUtils {
    // 字体是否使用DP单位----TypedValue.COMPLEX_UNIT_DIP : TypedValue.COMPLEX_UNIT_SP
    public static final boolean isUseDp = false;

    public static void setTextSize(TextView tv, int value) {
        tv.setTextSize(isUseDp ? TypedValue.COMPLEX_UNIT_DIP : TypedValue.COMPLEX_UNIT_SP, value);
    }

    /**
     * EditText竖直方向是否可以滚动
     * (文本高度超过控件高度或者文本长度超过控件宽度)
     *
     * @param editText 需要判断的EditText
     * @return true：可以滚动   false：不可以滚动
     */
    public static boolean canVerticalScroll(EditText editText) {
        //滚动的距离
        int scrollY = editText.getScrollY();
        //控件内容的总高度
        int scrollRange = editText.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() - editText.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;
        return scrollDifference != 0 && ((scrollY > 0) || (scrollY < scrollDifference - 1));
    }

    public static int getViewWidth(View view) {
        int width = view.getWidth();
        if (width == 0) {
            int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(size, size);
            width = view.getMeasuredWidth();
        }
        return width;
    }

    public static int getViewHeight(View view) {
        int height = view.getHeight();
        if (height == 0) {
            int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(size, size);
            height = view.getMeasuredHeight();
        }
        return height;
    }

    public static int[] getViewSize(View view) {
        int[] sizeArr = new int[]{view.getWidth(), view.getHeight()};
        if (sizeArr[0] == 0 || sizeArr[1] == 0) {
            int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            view.measure(size, size);
            if (sizeArr[0] == 0) sizeArr[0] = view.getMeasuredWidth();
            if (sizeArr[1] == 0) sizeArr[1] = view.getMeasuredHeight();
        }
        return sizeArr;
    }

    public static void setGradientTextColor(TextView textView, int startColor, int endColor) {
        LinearGradient mLinearGradient = new LinearGradient(0, 0, textView.getPaint().getTextSize() * textView.getText().length(), 0, startColor, endColor, Shader.TileMode.CLAMP);
        textView.getPaint().setShader(mLinearGradient);
        textView.invalidate();
    }

    // 设置RecyclerView列表空白处的点击响应
    private static float mScrollWorkX, mScrollWorkY;
    private static long mDownTime;
    private static boolean isLongPress;

    @SuppressLint("ClickableViewAccessibility")
    public static void setOnTouchRecyclerView(RecyclerView rcv, View.OnClickListener onClickListener, View.OnLongClickListener onLongClickListener) {
        rcv.setOnTouchListener((View v, MotionEvent event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                mScrollWorkX = event.getX();
                mScrollWorkY = event.getY();
                mDownTime = System.currentTimeMillis();
                isLongPress = false;
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                long curTime = System.currentTimeMillis();
                if (curTime - mDownTime >= 400 && Math.abs(mScrollWorkX - event.getX()) <= 5 && Math.abs(mScrollWorkY - event.getY()) <= 5) {
                    if (!isLongPress && onLongClickListener != null) {
                        isLongPress = onLongClickListener.onLongClick(v);
                        return isLongPress;
                    }
                }
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                if (!isLongPress && v.getId() != 0 && Math.abs(mScrollWorkX - event.getX()) <= 5 && Math.abs(mScrollWorkY - event.getY()) <= 5) {
                    //recyclerView空白处点击事件
                    if (onClickListener != null) onClickListener.onClick(v);
                }
            }
            return false;
        });
    }
}
